import sys
from math import pi
from random import choice

import pygame

from lib.menu import Title, RotatingMenu, MenuItem
from lib.game import Game
from lib.editor import Editor
from lib import language

DISPLAY_WIDTH = 640
DISPLAY_HEIGHT = 480
FPS_LIMIT = 90

DEFAULT_ARC = 320, 240, 270, pi*0.75, pi/2.0

class Main:
    def __init__(self):
        pygame.init()
        language.load("en-US")
        
        self.display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
        self.clock = pygame.time.Clock()
        
        self.title = Title(320, 80, language.get("Menu", "header"))
        
        self.menu = RotatingMenu(*DEFAULT_ARC)
        self.menuItem0 = self.menu.addItem(MenuItem(language.get("Menu", "exit"), self.menuExit))
        self.menuItem1 = self.menu.addItem(MenuItem(language.get("Menu", "options"), self.menuOptions))
        self.menuItem2 = self.menu.addItem(MenuItem(language.get("Menu", "start"), self.menuGame))
        self.menuItem3 = self.menu.addItem(MenuItem(language.get("Menu", "levels"), self.menuLevels))
        self.menuItem4 = self.menu.addItem(MenuItem(language.get("Menu", "editor"), self.menuEditorMenu))
        self.menu.selectItemImmediately(2)
        
        self.optionsMenu = RotatingMenu(*DEFAULT_ARC)
        self.optionsItem0 = self.optionsMenu.addItem(MenuItem("Resolution", self.optionsResolution))
        self.optionsItem1 = self.optionsMenu.addItem(MenuItem("Back", self.menuBack))
        self.optionsItem2 = self.optionsMenu.addItem(MenuItem("Resolution", self.optionsResolution))
        self.optionsMenu.selectItemImmediately(1)
        
        self.editorMenu = self.optionsMenu = RotatingMenu(*DEFAULT_ARC)
        self.editorItem0 = self.editorMenu.addItem(MenuItem("Load Level", self.menuEditor))
        self.editorItem1 = self.editorMenu.addItem(MenuItem("New Level", self.menuEditor))
        self.editorItem2 = self.editorMenu.addItem(MenuItem("Back", self.menuBack))
        self.editorMenu.selectItemImmediately(1)
        
        self.editor = Editor()
        self.editor.back = self.menuBack
        
        self.game = Game()
        self.game.win = self.win
        self.game.lose = self.lose
        
        self.context = self.menu
        
        #Loop
        while True:
            #Handle events
            events = pygame.event.get()
            
            #Update stuff
            self.context.update(events)
            
            #Draw stuff
            self.context.draw(self.display)
            if self.context == self.menu or self.context == self.optionsMenu:
                self.title.draw(self.display)
            
            pygame.display.flip() #Show the updated scene
            self.clock.tick(FPS_LIMIT) #Wait a little
    
    #Menu
    def menuGame(self):
        self.context = self.game
    
    def menuOptions(self):
        self.context = self.optionsMenu
    
    def menuExit(self):
        return sys.exit()
    
    def menuLevels(self):
        pass
    
    def menuEditor(self):
        self.context = self.editor
    
    def menuEditorMenu(self):
        self.context = self.editorMenu
    
    def menuBack(self):
        self.context = self.menu
    
    #Options Menu
    def optionsResolution(self):
        pass
    
    #Game
    def win(self):
        self.context = self.menu
        
        self.title.text = choice(language.getSection("WinStrings"))
        self.title.redrawText()
        self.menuItem2.text = language.get("Menu", "continue")
        self.menuItem2.redrawText()
        self.menuItem0.text = language.get("Menu", "exitWin")
        self.menuItem0.redrawText()
    
    def lose(self):
        self.context = self.menu
        
        self.title.text = choice(language.getSection("LoseStrings"))
        self.title.redrawText()
        self.menuItem2.text = language.get("Menu", "continue")
        self.menuItem2.redrawText()
        self.menuItem0.text = language.get("Menu", "exitLose")
        self.menuItem0.redrawText()

if __name__ == "__main__":
    Main()